package com.gxa.service.points.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.dto.pointsdto.ExchangeRecordDto;
import com.gxa.dto.pointsdto.IntegrationDto;
import com.gxa.entity.points.ExchangeRecord;
import com.gxa.entity.points.Integration;
import com.gxa.mapper.points.ExchangeRecordMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.mapper.points.IntegrationMapper;
import com.gxa.service.points.ExchangeRecordService;
import com.gxa.service.points.IntegrationService;
import com.gxa.vo.ExchangeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 礼品兑换记录表 服务实现类
 * </p>
 *
 * @author yjl
 * @since 2022-10-17 10:36:06
 */
@Service
public class ExchangeRecordServiceImpl extends ServiceImpl<ExchangeRecordMapper, ExchangeRecord> implements ExchangeRecordService {

    @Autowired
    private ExchangeRecordMapper exchangeRecordMapper;

    @Autowired
    private IntegrationMapper integrationMapper;

    @Override
    public PageInfo<ExchangeRecordDto> queryAll(Integer pageNum, Integer count) {
        Page<ExchangeRecordDto> page1 = PageHelper.startPage(pageNum,count);
        List<ExchangeRecordDto> exchangeRecordDtos = this.exchangeRecordMapper.queryAll();
        PageInfo<ExchangeRecordDto> pageInfo = new PageInfo<ExchangeRecordDto>(exchangeRecordDtos);

        return pageInfo;
    }

    @Override
    public ExchangeRecordDto queryById(Integer id) {
        ExchangeRecordDto exchangeRecordDto = this.exchangeRecordMapper.queryById(id);
        return  exchangeRecordDto;
    }


    @Override
    public void delete(Integer id,String userNumber, Integer score) {
        this.exchangeRecordMapper.deleteById(id);
        this.exchangeRecordMapper.upateByIdAndScor(userNumber,score);
    }

    @Override
    public boolean add(ExchangeRecordDto exchangeRecordDto) {
        IntegrationDto integrationDto = this.integrationMapper.queryByUserName(exchangeRecordDto.getUserNumber());
        if (integrationDto != null){
            this.exchangeRecordMapper.add(exchangeRecordDto);
            this.exchangeRecordMapper.updateByUserNumAndScore(exchangeRecordDto);
            return true;
        }else {
            return false;
        }

    }

    @Override
    public void updateByUserName(String userName, Date date) {
        this.exchangeRecordMapper.updateByUserName(userName, date);
    }

    @Override
    public PageInfo<ExchangeRecordDto> queryByVo(ExchangeVo exchangeVo) {
        System.out.println(exchangeVo);
        Page<ExchangeRecordDto> page1 = PageHelper.startPage(exchangeVo.getPagenum(),exchangeVo.getCount());
        List<ExchangeRecordDto> exchangeRecordDtos = this.exchangeRecordMapper.queryByVo(exchangeVo);
        PageInfo<ExchangeRecordDto> pageInfo = new PageInfo<ExchangeRecordDto>(exchangeRecordDtos);
        return pageInfo;
    }

    @Override
    public void updateById(Integer id,Date date) {
        this.exchangeRecordMapper.updateById(id,date);
    }


}
